//权限拦截在路由跳转导航守卫
import router from '@/router'
import store from '@/store'
import nprogress from 'nprogress'
import 'nprogress/nprogress.css'
//不需要导出，让代码执行即可
/*
next()放过
next(地址)到具体地址
next(false)终止跳转
*/

const whiteList = ['/login', '/404']

router.beforeEach((to, from, next) => {
    nprogress.start()//开启进度条
    if(store.getters.token){
        //如果有token
        if(to.path === '/login'){
            //如果要访问的是登录页
            next('/')
        }else{
            next()
        }
    }else{
        //没token if(whiteList.some)
        if(whiteList.indexOf(to.path) > -1){
            //要去的地址在白名单
            next()
        }else{
            next('/login')
        }
        
    }
    nprogress.done()//解决手动切换地址
})
router.afterEach(() => {
    nprogress.done()
})